<?php
/**
 * Created by PhpStorm.
 * User: zlkmu
 * Date: 2017/10/22
 * Time: 22:01
 * 麻将玩家的类
 */
require_once ('MajongManager.php');
require_once ('MajongCard.php');
require_once ('MajangLogic.php');
require_once ('MajangContext.php');

class MajangPlayer{
    public $playerID;
    //玩家的手牌
    public $playerPai = array();
    //玩家的碰的牌
    public $playerPeng = array();
    //玩家的杠的牌
    public $playerGang = array();
    //玩家打的牌
    public $playerChuPai = array();

    //玩家准备的状态
    public $isReady = false;

    //玩家牌的当前可能性
    public $canGang = false;
    public $canFu = false;
    public $canPeng = false;

    //玩家当前的状态
    public $isTui = false;  //是否推牌
    public $isHu = false;   //是否顶牌

    //用于判断麻将逻辑的对象
    private $majangLogic;

    //玩家本局可听牌组合，array  (i=>array(打的什么牌, array(a, b, c)具体胡的牌)
    private $playerCurTing = array();
    //玩家实际推的牌,保留玩家推的应胡牌, array(打的牌=>array(能胡的牌))
    private $playerTuiPai = array();


    function __construct($id)
    {
        $this->playerID = $id;
        $this->majangLogic = new MajangLogic();
    }

    function getPlayerID(){
        return $this->playerID;
    }

    function readyForPlay(){
        $this->isReady = true;
    }

    function isPlayerReady(){
        return $this->isReady;
    }

    function getPai(){
        return $this->playerPai;
    }

    /**
     * 摸牌函数
     * @param $pai就是一个整数
     * @return array('hu'=>true/false, 'gang'=> MING_GANG/AN_GANG/false, 'ting'=>array(i=>array(打什么牌, array(胡的牌)))
     */
    function moPai($pai){

        $returnArray = array();
        //检查摸牌以后能否杠，能否胡，能否听牌
        $returnArray['hu'] = $this->checkHu($pai);
        $returnArray['ting'] = $this->checkTing($pai);
        $returnArray['gang'] = $this->checkGang($pai);

        //将摸的牌放入玩家手牌
        array_push($this->playerPai, $pai);
        sort($this->playerPai);

        return $returnArray;
    }

    /**
     * 打牌函数
     * @param $pai,一个来自前端的数据，就是一个数值
     * @return $tempCard 打出去的具体牌，用于计算结果
     */
    function daPai($pai){
        if(!$this->checkPai($pai)){
            return false;
        }
        array_push($this->playerChuPai, $pai);
        for($i = 0; $i < count($this->playerPai); $i++){
            if($this->playerPai[$i] == $pai){
                array_splice($this->playerPai, $i, 1);
                return true;
            }
        }
        return false;
    }

    /**
     * 检查打牌是否合法的函数
     * @param $pai
     * @return bool
     */
    private function checkPai($pai){
        print_r($this->playerPai);
        for($i = 0; $i < count($this->playerPai); $i++){
            if($this->playerPai[$i] == $pai){
                return true;
            }
        }
        return false;
    }

    /**
     * @param $pai 某个玩家打的牌
     * @return 如果能碰，返回true, 否则返回false
     */
    function checkPeng($pai){
        return  $this->majangLogic->checkPeng($this->playerPai, $pai);
    }

    /**
     * @param $pai 某个玩家打的牌
     * @return true/false
     */
    function checkGang($pai){
        return $this->majangLogic->checkGang($this->playerPai, $this->playerPeng, $pai);
    }

    /**
     * @param $pai 某个玩家打的牌
     * @return 如果能胡，返回的是
     */
    function checkHu($pai){
        return $this->majangLogic->checkHu($this->playerPai, $pai);
    }

    /**
     * 检查玩家是否可听牌的函数
     * @param $pai 某个玩家的摸的牌
     * @return array 返回的听牌结果,array  (i=>array(打的什么牌, array(a, b, c)具体胡的牌)
     */
    function checkTing($pai){
        $this->playerCurTing = $this->majangLogic->canTingPai($this->playerPai, $pai);
        return $this->playerCurTing;
//        $tingPaiStr = "";
//        for($i = 0; $i < count($this->playerCurTing); $i++){
//            $tingPaiStr .= $this->playerCurTing[$i][0]. ",";
//            $tingPaiStr .= implode(",", $this->playerCurTing[$i][1]);
//            $tingPaiStr .= ";";
//        }
//        return $tingPaiStr;
    }

    /**
     * 玩家推牌的函数
     * @param $tuiRequest，前台传输过来的实际打的牌,仅有一张
     */
    function tuiPai($tuiRequest){
        //用逗号分隔字符串，第一个字符是打的牌，后面是胡的牌

        for($i = 0; $i < count($this->playerCurTing); $i++){
            if($this->playerCurTing[$i][0] == $tuiRequest){
                $this->playerTuiPai = $this->playerCurTing[$i][1];
                break;
            }
        }
        return $this->playerTuiPai;
    }


    /**
     * 初始化玩家手牌的函数
     * @param $pai 来自发牌函数的牌
     */
    function setPai($pai){
        $this->playerPai = $pai;
        //排序
        sort($this->playerPai);

    }

    /**
     * 玩家碰牌的函数
     * @param $pai，牌
     * @return bool，返回true/false
     */
    function pengPai($pai){
        for($i = 0; $i < count($this->playerPai) - 1; $i++){
            if(($this->playerPai[$i] == $pai) && ($this->playerPai[$i+1] == $pai)){
                //echo $i;
                //print_r($this->playerPai);
                array_splice($this->playerPai, $i, 2);
                array_push($this->playerPeng, $pai);
                //print_r($this->playerPai);
                return true;
            }
        }
        return false;
    }

    /**
     * 杠牌函数
     * @param $pai 杠的牌
     * @return int 0:不能杠或者错误，1：暗杠， 2：明杠
     */
    function gangPai($pai){
        for($i = 0; $i < count($this->playerPai) - 2; $i++){
            if(($this->playerPai[$i] == $pai) && ($this->playerPai[$i+1] == $pai) && ($this->playerPai[$i+2] == $pai)){
                array_splice($this->playerPai, $i, 3);
                array_push($this->playerGang, $pai);
                return AN_GANG;
            }
        }

        for($i = 0; $i < count($this->playerPeng); $i++){
            if($this->playerPeng[$i] == $pai){
                array_splice($this->playerPeng, $i, 1);
                array_push($this->playerGang, $pai);
                return MING_GANG;
            }
        }
        return DATA_ERROR;

    }

    function huPai($pai){
        return 1;
    }



    function setPeng($pengRequest){

    }

    function getPeng(){
        return $this->playerPeng;
    }

    function setGang($gangRequest){

    }

    function getGang(){
        return $this->playerGang;
    }

}


?>